/*
 * Copyright Hundsun Technologies Inc. All Rights Reserved.
 *
 * @author caijk17241
 * @date 2021-2-3
 */

package rabbitmq

import (
	"github.com/pkg/errors"
	"github.com/streadway/amqp"
)

const (
	// ExchangeDirect Constants for standard AMQP 0-9-1 exchange types.
	ExchangeDirect = amqp.ExchangeDirect
)

// ExchangeDeclare return exchange declare handler function option
func ExchangeDeclare(name, kind string, args amqp.Table) HandlerFunc {
	durable := false
	autoDelete := false
	internal := false
	noWait := false

	return func(c Connection) error {
		err := c.ExchangeDeclare(name, kind, durable, autoDelete, internal, noWait, args)
		if err != nil {
			return errors.Wrap(err, "unable to declare exchange")
		}

		return nil
	}
}

// QueueDeclare return queue declare handler function option
func QueueDeclare(name string, args amqp.Table) HandlerFunc {
	// 持久化
	durable := true
	// 无消费者时，自动删除队列
	autoDelete := true
	exclusive := false
	noWait := false

	return func(c Connection) error {
		err := c.QueueDeclare(name, durable, autoDelete, exclusive, noWait, args)
		if err != nil {
			return errors.Wrap(err, "unable to declare queue")
		}

		return nil
	}
}

// QueueBind binding queue with exchange
func QueueBind(name, key, exchange string, noWait bool, args amqp.Table) HandlerFunc {
	return func(c Connection) error {
		err := c.QueueBind(name, key, exchange, noWait, args)
		if err != nil {
			return errors.Wrap(err, "unable to bind queue")
		}

		return nil
	}
}
